package com.kl.modular.sysman.system.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.plugins.Page;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.common.controller.BaseController;
import com.kl.modular.common.response.ResponseBase;
import com.kl.modular.system.dao.model.ArticleType;
import com.kl.modular.system.dao.model.SystemDic;
import com.kl.modular.system.service.api.IArticleTypeService;
import com.kl.modular.system.service.api.ISystemDicService;
import com.kl.modular.system.service.bo.ArticleTypeBo;
import org.apache.commons.lang.StringUtils;
import javax.validation.Valid;
import org.springframework.validation.BindingResult;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 文章类型控制器
 * @author xr_Yifan
 * @date 2017-12-12 08:52
 * @(C) xinrui Copyright 
 */
@Controller
public class SystemArticleController extends BaseController {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemArticleController.class);
	
	@Autowired
	private IArticleTypeService articleTypeService;

	@Autowired
	private ISystemDicService systemDicService;

	/** 文章类型 view */
	private static String pathArticleTypeRoot="/sysman/system/article/";


	/**
	 * 检查项目列表选择页面
	 */
	@RequestMapping(value = "/sysman/articles/toArticleTypePopup", method = RequestMethod.GET)
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:view")
	public String showArticleTypePopup( HttpServletRequest request) throws Exception {
		logger.info("检查项目列表选择页面");
		ArticleType articleType = new ArticleType();
		articleType.setParentId(0L);
		List<ArticleType> articleTypeList = articleTypeService.selectListByEntity(articleType);
		request.setAttribute("articleTypeList", JSON.toJSONString(articleTypeList));
		return pathArticleTypeRoot + "articleTypePopup.ftl";

	}

	/**
	 * 文章类型数据
	 * @param parentId
	 * @return
	 */
	@RequestMapping(value = "/sysman/articles/popupArticleTypeList" , method = RequestMethod.POST,produces = "text/html;charset=utf-8")
	@ResponseBody
	public String findArticleTypeList(Long parentId){
		if(parentId == null){
			parentId = 0L;
		}

			ArticleType articleType = new ArticleType();
			articleType.setParentId(parentId);
			List<ArticleType> articleTypeList = articleTypeService.selectListByEntity(articleType);
			return JSON.toJSONString(articleTypeList);
	}

	/**
	 * 文章类型列表页面
	 */
	@RequestMapping(value = "/sysman/articles/articleTypeList", method = RequestMethod.GET)
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:view")
	public String queryList(HttpServletRequest request) {
		logger.info("文章类型列表页面");
		ArticleType articleType = new ArticleType();
		articleType.setParentId(0L);
		List<ArticleType> articleTypeList = articleTypeService.selectListByEntity(articleType);
		request.setAttribute("articleTypeList", JSON.toJSONString(articleTypeList));
		return pathArticleTypeRoot + "articleTypeList.ftl";
	}

	/**
	 * 文章类型列表数据
	 */
	@RequestMapping(value = "/sysman/articles/articleTypeAjaxList", method = RequestMethod.POST)
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:view")
	public String queryAjaxList(ArticleTypeBo articleTypeBo, Page<ArticleType> page, HttpServletRequest request) {
		logger.info("articleType列表数据>>data:" + JSON.toJSONString(articleTypeBo));
		page = articleTypeService.selectPageByBo(page, articleTypeBo);
		request.setAttribute("pager", page);
		Map<String, SystemDic> typeCodes = systemDicService.selectMapByDicType("ARTICLE_TYPE");
		request.setAttribute("typeCodes", typeCodes);
		return pathArticleTypeRoot + "articleTypeAjaxList.ftl";
	}
	
	
	/**
	 * 文章类型添加页面
	 * @return
	 */
	@RequestMapping(value="/sysman/articles/toArticleTypeAdd" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:add")
	public String toAddArticleType(@RequestParam Long parentId,HttpServletRequest request){
		logger.info("文章类型添加页面");
        ArticleType articleType = new ArticleType();
        articleType.setParentId(parentId);
		Map<String, SystemDic> typeCode = systemDicService.selectMapByDicType("ARTICLE_TYPE");
        request.setAttribute("articleType", articleType);
		request.setAttribute("typeCode", typeCode);
		return pathArticleTypeRoot + "addArticleType.ftl";
	}
	
	/**
	 * 添加文章类型
	 */
	@RequestMapping(value="/sysman/articles/addArticleType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:add")
	public String addArticleType(@Valid ArticleType articleType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("添加文章类型>>data:" + JSON.toJSONString(articleType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}

		articleType.setModifyAdminId(Long.valueOf(getAuthUserId()));
		articleTypeService.insert(articleType);

		String jsonstr = ResponseBase.success();
		logger.info("添加文章类型>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	
	/**
	 * 文章类型更新页面
	 * @return
	 */
	@RequestMapping(value = "/sysman/articles/toChangeView" , method = RequestMethod.GET)
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:update")
	public String toChangeArticleType(Long id, HttpServletRequest request) throws Exception{
		logger.info("文章类型修改页面>>data:" + id);
		ArticleType articleType = articleTypeService.selectById(id);
		request.setAttribute("articleType", articleType);
		Map<String, SystemDic> typeCode = systemDicService.selectMapByDicType("ARTICLE_TYPE");
		request.setAttribute("typeCode", typeCode);
		return pathArticleTypeRoot + "articleTypeUpChange.ftl";
	}

	
	/**	
	 * 修改
	 */
	@RequestMapping(value = "/sysman/articles/upChange" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:update")
	public String upChangeArticleType(@Valid ArticleType articleType, BindingResult result, HttpServletResponse response) throws Exception{
		logger.info("更新文章类型信息>>data:" + JSON.toJSONString(articleType));

		if (result.hasErrors()) {
			return ResponseBase.errorStr(result.getAllErrors().get(0).getDefaultMessage());
		}
		/*if(articleType.getId() == null){
			return ResponseBase.errorStr(ResponseBase.RESPOSNE_NOT_VALID_ERROR);
		}*/

		articleType.setModifyTime(new Date());
		articleTypeService.updateById(articleType);

		String jsonstr = ResponseBase.success();
		logger.info("编辑文章类型信息>>返回>>data:" + jsonstr);
		return jsonstr;
	}

	/**
	 * 批量删除文章类型
	 */
	@RequestMapping(value = "/sysman/articles/delArticleType" , method = RequestMethod.POST)
	@ResponseBody
	@RequiresPermissions("sysman:ARTICLETYPE_MAN:delete")
	public String delArticleType(String id,HttpServletResponse response) throws Exception{
		logger.info("删除文章类型>>data:" + JSON.toJSONString(id));

		ResponseBase base = new ResponseBase();

		// 数据验证
		if (StringUtils.isEmpty(id)) {
			logger.info("数据验证>>删除文章类别>>id is null");
			base.setMsg("参数错误，请选择要删除的文章类别");
			return JSON.toJSONString(base);
		}

		String[] codestrs = id.split(",");

		if(codestrs == null || codestrs.length == 0){
			logger.info("数据验证>>删除文章类别>>codestrs is null");
			base.setMsg("参数错误，请选择要删除的文章类别");
			return JSON.toJSONString(base);
		}

		articleTypeService.deleteByArticleTypeId(codestrs[0]);

		base.setStatus(ResponseBase.RESPONSE_STATUS_SUCCESS);
		String jsonstr = JSON.toJSONString(base);
		logger.info("删除文章类型资源>>返回view>>data:" + jsonstr);
		return jsonstr;
	}
}
